package cn.tpshion.vm.common.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class AESUtil {

    public static String encrypt(String content, String secret) {
        byte[] key = Base64.getDecoder().decode(secret);
        AES aes = SecureUtil.aes(key);
        byte[] encrypt = aes.encrypt(content);
        return Base64.getEncoder().encodeToString(encrypt);
    }

    public static String decrypt(String content, String secret) {
        byte[] key = Base64.getDecoder().decode(secret);
        AES aes = SecureUtil.aes(key);
        byte[] decrypt = aes.decrypt(Base64.getDecoder().decode(content));
        return new String(decrypt, StandardCharsets.UTF_8);
    }

    public static String generateKey(){
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
        return Base64.getEncoder().encodeToString(key);
    }

}
